package com.bgt.springboot.first.repository;

import com.bgt.springboot.first.utils.Page;
import com.bgt.springboot.first.vo.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by bgt on 2017/4/18.
 */
@Repository
public interface UserRepository extends CrudRepository<User,Integer> {
    @Query(value = "select * from User",nativeQuery = true)
    List<User> findAll();
    @Query(value = "select count(*) from User",nativeQuery = true)
    int countAll();
    @Query("select n from User n where n.id=?1")
     List<User> findById(Integer id);
   @Query("from User n where n.id =:cn")
     List<User> findById2(@Param("cn")Integer cn);

    //后边的都是示例代码 未经过测试
    //原生的mysql 也可以用jpal 面向对象的编程
    @Query(value = "select u.* from userinfo u where u.name = ?1", nativeQuery = true)
    User findByname(String name);

    // 修改
    @Modifying
    @Query(value = "update User set name=?1 where name=?2")
    void updateUser(String newName, String oldName);

    // 删除
    @Modifying
    @Query(value = "delete from User where id=?1")
    void delete(Integer id);
}
